/**
 * 
 */
package com.tmc.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.entity.common.EthUser;
import com.tmc.service.common.LoginServiceImpl;
import com.tmc.util.ImageCode;
import com.tmc.web.BaseController;

@Controller
public class LoginController extends BaseController {

	@Autowired
	private LoginServiceImpl loginService;
	
	@Autowired
	private ImageCode imageCode;
	
	@Value("${ImageCode.sessionKey}")
	private String sessionKey;

	@RequestMapping(value = "/login-page", method = RequestMethod.GET)
	public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		return new ModelAndView("login", map);
	}
	
	@RequestMapping(value = "/login-code", method = RequestMethod.POST)
	@ResponseBody
	public String login(String userCode, String password, HttpServletRequest request, HttpServletResponse response) {
		CommonResult cr = loginService.login(userCode, password);
		if (cr.isSuccess()) {
			this.saveCookie((String) cr.getObj(), request, response);
			ContextInfo context = loginService.getContextInfoByToken((String) cr.getObj());
			return CommonResult.makeSuccess("", context).toJson();
		} else {
			return cr.toJson();
		}
	}
	
	@RequestMapping(value = "/index-page", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		return new ModelAndView("index", map);
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpServletResponse response){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = loginService.logout(context);
		if (cr.isSuccess()){
			this.deleteCookie(context.getToken(), request, response);
		}
		return "login";
	}
	
	private void deleteCookie(String token, HttpServletRequest request, HttpServletResponse response) {
		Cookie cookie = new Cookie("token", null);
		cookie.setDomain(request.getServerName());
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	private void saveCookie(String token, HttpServletRequest request, HttpServletResponse response) {
		Cookie cookie = new Cookie("token", token);
		cookie.setDomain(request.getServerName());
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	@RequestMapping(value = "/user/register", method = RequestMethod.POST)
	public String register(EthUser user) {
		loginService.addUser(user);
		return "login";
	}

	@RequestMapping(value = "/user/toRegister", method = RequestMethod.GET)
	public String toRegister() {
		return "register";
	}
	
	@RequestMapping(value = "/login-getImage")
	public void getImage(HttpServletRequest request, HttpServletResponse response, String random) throws IOException {
		// 禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 将图像输出到Servlet输出流中。
		ServletOutputStream sos = response.getOutputStream();
		ImageIO.write(imageCode.getImage(request), "jpeg", sos);
		sos.close();
	}
	
}
